from QAWebServer.basehandles import QABaseHandler
import QUANTAXIS as QA
import datetime

class SelectCodehandler(QABaseHandler):

    """


    topic: 选股主题
    date: 选股日期
    name: 主题名称
    market: 选股市场
    codelist: 选股的股票


    """
    stocklist= QA.QA_fetch_stock_list_adv()
    def get(self):
        c = QA.DATABASE.selectedCode
        req = self.get_argument('action')
        if req == 'get_list':
            # http://localhost:8022/selected?action=get_list
            u =  [i for i in c.find({}, {'_id':0, 'topic': 1, 'name': 1})]
            
            self.write({'result': dict(zip([i['topic'] for i in u ], [i['name'] for i in u]))})
        
        elif req == 'get_selected':
            # http://localhost:8022/selected?action=get_selected&topic=x1

            #{"result": {"topic": "x1", "name": "\u9009\u80a1\u6d4b\u8bd5", "date": "2020-04-24", "market": "stock_cn", "codelist": ["000001", "000016", "600000"]}}
            rt = self.get_argument('topic')
            codelist =  c.find_one({'topic':rt}, {'_id':0})
            if codelist:
              codelist['codelist'] = QA.QA_util_to_json_from_pandas(QA.QA_fetch_stock_list_adv().loc[codelist['codelist']].loc[: , ['code','name']])
              self.write({'result': codelist})
        
        elif req == 'get_selectindex':
            # http://localhost:8022/selected?action=get_selectindex&topic=x1

            """
            {"result": [{"date": "2020-01-22 00:00:00", "0": 1000.0}, 
                        {"date": "2020-01-23 00:00:00", "0": 967.3166181691}, 
                        {"date": "2020-02-03 00:00:00", "0": 881.8904197598}, 
                        {"date": "2020-02-04 00:00:00", "0": 909.6074122231}, 
                        {"date": "2020-02-05 00:00:00", "0": 914.1033957549}, 
                        {"date": "2020-02-06 00:00:00", "0": 924.1628102945}, 
                        {"date": "2020-02-07 00:00:00", "0": 925.1503083038}, 
                        {"date": "2020-02-10 00:00:00", "0": 920.205199527}, 
                        {"date": "2020-02-11 00:00:00", "0": 934.147057338}, 
                        {"date": "2020-02-12 00:00:00", "0": 936.4459239026}, 
                        {"date": "2020-02-13 00:00:00", "0": 926.5638698595}, 
                        {"date": "2020-02-14 00:00:00", "0": 942.6908770907}, {"date": "2020-02-17 00:00:00", "0": 966.0848243186}, {"date": "2020-02-18 00:00:00", "0": 958.8774518336}, {"date": "2020-02-19 00:00:00", "0": 963.8502101934}, {"date": "2020-02-20 00:00:00", "0": 982.7194434143}, {"date": "2020-02-21 00:00:00", "0": 989.3145983522}, {"date": "2020-02-24 00:00:00", "0": 970.7196037072}, {"date": "2020-02-25 00:00:00", "0": 958.9718485897}, {"date": "2020-02-26 00:00:00", "0": 962.0591420838}, {"date": "2020-02-27 00:00:00", "0": 962.4153739617}, {"date": "2020-02-28 00:00:00", "0": 931.2356193706}, {"date": "2020-03-02 00:00:00", "0": 949.5281017781}, {"date": "2020-03-03 00:00:00", "0": 951.0742708742}, {"date": "2020-03-04 00:00:00", "0": 945.901373953}, {"date": "2020-03-05 00:00:00", "0": 982.5224086738}, {"date": "2020-03-06 00:00:00", "0": 964.999906203}, {"date": "2020-03-09 00:00:00", "0": 932.4367209624}, {"date": "2020-03-10 00:00:00", "0": 949.4416669504}, {"date": "2020-03-11 00:00:00", "0": 943.7446579637}, {"date": "2020-03-12 00:00:00", "0": 933.6136247329}, {"date": "2020-03-13 00:00:00", "0": 927.6828049694}, {"date": "2020-03-16 00:00:00", "0": 895.9372966734}, {"date": "2020-03-17 00:00:00", "0": 883.7909410019}, {"date": "2020-03-18 00:00:00", "0": 852.4716828429}, {"date": "2020-03-19 00:00:00", "0": 825.1955625667}, {"date": "2020-03-20 00:00:00", "0": 838.7105536577}, {"date": "2020-03-23 00:00:00", "0": 816.6175242377}, {"date": "2020-03-24 00:00:00", "0": 838.6012646284}, {"date": "2020-03-25 00:00:00", "0": 850.5934812712}, {"date": "2020-03-26 00:00:00", "0": 858.6054897572}, {"date": "2020-03-27 00:00:00", "0": 861.1864881852}, {"date": "2020-03-30 00:00:00", "0": 856.0590637612}, {"date": "2020-03-31 00:00:00", "0": 846.5121400999}, {"date": "2020-04-01 00:00:00", "0": 847.6778794969}, {"date": "2020-04-02 00:00:00", "0": 856.3347161786}, {"date": "2020-04-03 00:00:00", "0": 841.8654752161}, {"date": "2020-04-07 00:00:00", "0": 855.9743264087}, {"date": "2020-04-08 00:00:00", "0": 851.6073956976}, {"date": "2020-04-09 00:00:00", "0": 848.1169003544}, {"date": "2020-04-10 00:00:00", "0": 847.3066792092}, {"date": "2020-04-13 00:00:00", "0": 836.8678113239}, {"date": "2020-04-14 00:00:00", "0": 851.3433510566}, {"date": "2020-04-15 00:00:00", "0": 847.8430156341}, {"date": "2020-04-16 00:00:00", "0": 841.1314815529}, {"date": "2020-04-17 00:00:00", "0": 847.8969084543}, {"date": "2020-04-20 00:00:00", "0": 847.6408332376}]}
            """
            rt = self.get_argument('topic')
            codelist =  c.find_one({'topic':rt}, {'_id':0})
            end = QA.QA_util_get_real_date(str(datetime.date.today()))
            start  = QA.QA_util_get_last_day(end, n= 60)
            if codelist:
                block = QA.QAAnalysis_block(codelist['codelist'], start = start, end= end)
                ind = block.block_index()
                
                x = QA.QA_util_to_json_from_pandas(ind.reset_index().dropna())
                
                self.write({'result': x})
        


if __name__ == '__main__':
    fakecode = ['000001', '000016', '600000']
    struct = {
        'topic':'x1',
        'name': '选股测试',
        'date': '2020-04-24',
        'market': 'stock_cn',
        'codelist': fakecode,
    }

    QA.DATABASE.selectedCode.insert_one(struct)